import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'event_inquiry_page.dart';

class OctoberPage extends StatelessWidget {
  const OctoberPage({super.key});

  @override
  Widget build(BuildContext context) {
    return _GuitarShowcaseScreen(context);
  }
}

class _GuitarShowcaseScreen extends StatelessWidget {
  final BuildContext parentContext;

  const _GuitarShowcaseScreen(this.parentContext);

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
        image: DecorationImage(
          image: AssetImage('assets/images/my-bg.png'),
          fit: BoxFit.cover,
        ),
      ),
      child: Scaffold(
        backgroundColor: Colors.transparent,
        body: SafeArea(
          bottom: false,
          child: Column(
            children: [
              _createTopNavigationBar(),
              Expanded(
                child: SingleChildScrollView(
                  padding: EdgeInsets.symmetric(horizontal: 0.w),
                  child: _originalCompositionLayout(),
                ),
              ),
              _buildSignUpButton(),
            ],
          ),
        ),
      ),
    );
  }

  Widget _createTopNavigationBar() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 10.h),
      child: Row(
        children: [
          GestureDetector(
            onTap: () => Navigator.of(parentContext).pop(),
            child: Icon(
              Icons.arrow_back_ios_new,
              color: Colors.white,
              size: 20.w,
            ),
          ),
          Expanded(
            child: Center(
              child: Text(
                'Competition Rules',
                style: TextStyle(
                  fontSize: 22.sp,
                  fontWeight: FontWeight.w600,
                  color: Colors.white,
                  fontFamily: 'Poppins',
                ),
              ),
            ),
          ),
          SizedBox(width: 26.w),
        ],
      ),
    );
  }

  Widget _buildSignUpButton() {
    return Container(
      margin: EdgeInsets.all(20.w),
      child: SizedBox(
        width: double.infinity,
        height: 50.h,
        child: ElevatedButton(
          onPressed: () {
            Navigator.of(parentContext).push(
              MaterialPageRoute(
                builder:
                    (context) => const EventInquiryPage(eventType: 'october'),
              ),
            );
          },
          style: ElevatedButton.styleFrom(
            backgroundColor: const Color(0xFFCD8B76),
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(25.r),
            ),
            elevation: 0,
          ),
          child: Text(
            'Sign Up',
            style: TextStyle(
              fontSize: 18.sp,
              fontWeight: FontWeight.w600,
              color: Colors.white,
              fontFamily: 'Poppins',
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildExpandableFloatingButton() {
    return FloatingActionButton(
      onPressed: () {
        // 实现展开功能
      },
      backgroundColor: const Color(0xFFCD8B76),
      child: Icon(Icons.add, color: Colors.white, size: 24.w),
    );
  }

  Widget _originalCompositionLayout() {
    return _SoloPerformanceContainer();
  }
}

class _SoloPerformanceContainer extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        ..._createPaperBackgrounds(),
        Column(children: [_guitarCompetitionDetails(), SizedBox(height: 40.h)]),
      ],
    );
  }

  List<Widget> _createPaperBackgrounds() {
    final backgroundLayers = [
      {'top': 0.0, 'left': -0.0, 'right': -0.0},
      {'top': 620.0, 'left': 0.0, 'right': 0.0},
      {'top': 1200.0, 'left': 0.0, 'right': 0.0},
      {'top': 1800.0, 'left': 0.0, 'right': 0.0},
    ];

    return backgroundLayers.map((layer) {
      return Positioned(
        top: layer['top']!.h,
        left: layer['left']!.w,
        right: layer['right']!.w,
        child: Container(
          width: 446.w,
          height: 667.h,
          decoration: const BoxDecoration(
            image: DecorationImage(
              image: AssetImage('assets/images/paper-bg.png'),
              fit: BoxFit.contain,
            ),
          ),
        ),
      );
    }).toList();
  }

  Widget _guitarCompetitionDetails() {
    return _OriginalMusicShowcase();
  }
}

class _OriginalMusicShowcase extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(24.w),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          SizedBox(height: 20.h),
          _displayShowcaseTitle(),
          SizedBox(height: 20.h),
          _showcaseImageContainer(),
          SizedBox(height: 20.h),
          _renderShowcaseContent(),
        ],
      ),
    );
  }

  Widget _displayShowcaseTitle() {
    return Text(
      'The Ultimate Guitar Showdown',
      style: TextStyle(
        color: Colors.white,
        fontSize: 15,
        fontFamily: 'Porter Sans Block',
        fontWeight: FontWeight.w400,
      ),
    );
  }

  Widget _showcaseImageContainer() {
    return Container(
      width: double.infinity,
      height: 200.h,
      decoration: BoxDecoration(
        image: const DecorationImage(
          image: AssetImage('assets/images/october-image.png'),
          fit: BoxFit.cover,
        ),
      ),
    );
  }

  Widget _renderShowcaseContent() {
    return Markdown(
      data: _getSoloGuitarCompetitionRules(),
      styleSheet: _buildShowcaseStyleSheet(),
      physics: const NeverScrollableScrollPhysics(),
      shrinkWrap: true,
    );
  }

  MarkdownStyleSheet _buildShowcaseStyleSheet() {
    return MarkdownStyleSheet(
      p: TextStyle(
        color: Colors.white,
        fontSize: 12.sp,
        fontFamily: 'Poppins',
        fontWeight: FontWeight.w400,
      ),
      h1: TextStyle(
        color: Colors.white,
        fontSize: 15.sp,
        fontFamily: 'Poppins',
        fontWeight: FontWeight.w700,
      ),
      h2: TextStyle(
        color: Colors.white,
        fontSize: 12.sp,
        fontFamily: 'Poppins',
        fontWeight: FontWeight.w400,
      ),
    );
  }

  String _getSoloGuitarCompetitionRules() {
    return '''
# 1. Original Composition Performance

• Contestants perform their original compositions, either solo or with a backing band.

• The performance must not exceed 5 minutes.

• Participants are encouraged to submit their sheet music or demo recording beforehand.

**Scoring Focus:**

• **Creativity**: Unique structure, chord progressions, and melodies.
• **Musicality**: Technical execution combined with emotional storytelling.
• **Original Impact**: Memorability and innovation of the composition.

# 2. General Rules

**Participation Requirements**

## 1. Contestants must ensure their guitars are properly tuned before the performance.
## 2. Performances over the allocated time will result in point deductions.
## 3. Interruptions during performance due to non-essential adjustments will affect scores.

# 3. Competition Stages

## 1. Preliminary Round (Initial Selection):
• Contestants perform in smaller groups, and judges select top players to advance.

## 2. Final Round (Grand Showdown):
• Advanced players compete with longer performances or more challenging songs.

• Audience votes are included as additional bonus points.

# 4. Rewards and Recognition

## (1) Awards

**a. Best Player Award**: High-quality guitars and accessories (e.g. capo, pedal, strings).

**b. Creativity Award**: Free courses on composition or improvisation.

**c. Audience Choice Award**: Trophy and digital badge in the App.
''';
  }
}
